import Vue from 'vue'
import App from './admin.vue'
import VueResource from 'vue-resource'
import VueRouter from 'vue-router'
import routes from './router/index.js'
import nprogress from 'nprogress'
import iView from 'iview'
import 'lib-flexible'         // 引入 lib-flexible + rem 实现移动端屏幕适配
import storage from 'utils/helpers/storageLite'

import 'nprogress/nprogress.css'
import 'iview/dist/styles/iview.css'

Vue.use(VueResource)
Vue.use(VueRouter)
Vue.use(iView)

Vue.config.productionTip = false;

const router = new VueRouter({
  routes: routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    storage.remove('user');
  }
  let user = storage.getParse('user');
  if (!user && to.path !== '/login') {
    next({path: '/login'});
  } else {
    nprogress.start();
    next();
  }
});

router.afterEach((to, from) => {
  nprogress.done();
})

new Vue({
  el: '#app',
  router: router,
  render: h => h(App)
})
